package com.shenglue.ssi.dao;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.shenglue.ssi.domain.BuyInMain;
import com.shenglue.ssi.domain.Warehouse;

public  class WarehouseDaoImpl extends SqlMapClientDaoSupport implements WarehouseDao {

	/**
	 * 查询所有经销商信息
	 */
	public List<Warehouse> selectAllWarehouse() {
		@SuppressWarnings("unchecked")
		List<Warehouse> list = this.getSqlMapClientTemplate().queryForList(
		"selectAllWarehouse");

		if (list != null && list.size() > 0) {
			return list;
		} else {
			return null;
		}
	}

	/**
	 * 增加经销商记录
	 */
	public boolean saveWarehouse(Warehouse supp) {

		try {
			Object oRtn = this.getSqlMapClientTemplate().insert( "insertWarehouse", supp );
			// 已改正 来自网上的写法。已有文章讨论

			//插入成功 返回true
			return true;
		}
		catch ( DataAccessException e ) {
			//失败 返回false
			return false;
		}
	}


	/**
	 * 删除经销商记录
	 */
	public boolean deleteWarehouse(int suppId) {
		int flag= this.getSqlMapClientTemplate().delete("deleteWarehouseById", suppId);
		if( flag > 0 ) {
			return true;
		}
		return false;
	}

	/**
	 * 修改经销商记录
	 */
	public boolean updateWarehouse(Warehouse supp) {

		int flag = this.getSqlMapClientTemplate().update("updateWarehouse", supp);
		if( flag > 0 ) {
			return true;
		}else{
			return false;
		}
	}

	/**
	 * 为修改，根据 Id 查一条 入库单主表记录
	 */
	public Warehouse getOneWarehouseForUpdate( int suppId ) {
		Object oWarehouse = this.getSqlMapClientTemplate().
		queryForObject( "getOneWarehouseForUpdate", suppId );

		Warehouse supp = (Warehouse)oWarehouse;

		return supp;

	}	
	
}
